﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RpgMVX_SavesManager
{
    public partial class MainForm : Form
    {
        private string lastSelect = "";
        private static Settings fS = new Settings();
        private static FileSystemWatcher fsw;
        public MainForm()
        {
            InitializeComponent();
        }

        public static void reopenSettting()
        {
            fS.Close();
            fS = new Settings();
            fS.Show();
        }

        private void openSettings_Click(object sender, EventArgs e)
        {
            fS = new Settings();
            fS.Show();
        }

        private void chooseGameFolder_Click(object sender, EventArgs e)
        {
            SelectGameFolder sgf = new SelectGameFolder();
            sgf.Show();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            GetSetSettings.initalize(Application.StartupPath + "config.ini");
            //FileSystemWatcher fsw = new FileSystemWatcher();
            if (Setting.gameFolder == "")
            {
                SelectGameFolder sgf = new SelectGameFolder();
                if (sgf.ShowDialog() == DialogResult.Cancel) Application.Exit();
            }
            fsw = new FileSystemWatcher(Setting.gameFolder);
            fsw.Changed += (src,fse) => {
                FileUtils fu = new FileUtils(fse.FullPath);
                string fp, name;
                if (Setting.popupG)
                {
                    popUp_AskName pa = new popUp_AskName();
                    if(pa.ShowDialog() == DialogResult.OK)
                    {
                        name = pa.custName.Text;
                    }
                    else
                    {//Use Default Name
                    }
                }
                else if (Setting.autoBackupG)
                {//Use Default Name
                }
                else if (Setting.selfNamingG)
                {
                    name = Utils.nameParser(fse.Name);
                }
                else
                {//Use Default Name
                }
                if (Setting.bGameFolderG)
                {

                }
            };
        }

        private void gameSaves_SelectedIndexChanged(object sender, EventArgs e)
        {
            lastSelect = "GS";
            if (!this.deleteItem.Enabled) this.deleteItem.Enabled = true;
        }

        private void backupedSaves_SelectedIndexChanged(object sender, EventArgs e)
        {
            lastSelect = "BS";
            if (!this.deleteItem.Enabled) this.deleteItem.Enabled = true;
        }

        private void deleteItem_Click(object sender, EventArgs e)
        {
            if(lastSelect == "BS")
            {
                this.backupedSaves.ClearSelected();
            }else if (lastSelect == "GS")
            {
                this.gameSaves.ClearSelected();
            }
        }

        private void exitForm_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void daemonThread_Tick(object sender, EventArgs e)
        {
            //Why did I create this timer???
        }
    }
}
